package com.yycx.module.bus.provider.handler.haiying.policy;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.user.client.entity.AppPosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 招募人政策列表
 *
 * @author zyf
 */
@Component
public class PolicyZmSaleManPageListHandler implements PageInterceptor<BusPolicySaleman> {

    @Autowired
    private BusSalemanService salemanService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {

        cq.select(BusPolicySaleman.class, "*");
        cq.like(BusSaleman.class,"saleManName");
        cq.eq(BusPolicySaleman.class, "policyDrugId");
        cq.eq("saleman.saleManType",2);
        cq.addSelect(BusSaleman.class, "saleManName","saleManBusinessNo");
        cq.addSelect(BusDept.class, "deptName");
        cq.addSelect(AppPosition.class, "positionName");
        cq.orderByDesc("bps.createTime");
        cq.createJoin(BusSaleman.class);
        cq.createJoin(AppPosition.class).setMainField("positionId").setMainAlias("saleman").setJoinField("positionId").setJoinAlias("position");
        cq.createJoin(BusDept.class);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPolicySaleman> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Long saleManIdE=entityMap.getLong("saleManIdE");
            if(FlymeUtils.isNotEmpty(saleManIdE)){
                BusSaleman saleman=salemanService.getById(saleManIdE);
                entityMap.put("saleManNameE",saleman.getSaleManName());
            }
        }
        return PageInterceptor.super.complete(cq, result, extra);
    }
}
